package com.ktjy.crm.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ktjy.crm.entity.User;
import com.ktjy.crm.mapper1.UserMapper;
import com.ktjy.crm.service.IUserService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Service
public class UserServiceImpl implements IUserService {
    @Resource
    private UserMapper userMapper;

    @Override
    public User login(String usrName, String usrPassword) {
        User user = userMapper.getUserByUsrNameWithRoleName(usrName);
        if (user != null){ //判断用户是否存在
            if (user.getUsrPassword().equals(usrPassword)){ //判断密码是否正确
                return user;
            }
        }
        return null;
    }

    @Override
    public int addUser(User user) {
        return userMapper.insert(user);
    }

    @Override
    public int deleteUser(Long userId) {
        return userMapper.deleteById(userId);
    }

    @Override
    public int updateUser(User user) {
        return userMapper.updateById(user);
    }

    @Override
    public User getUser(Long usrId) {
        return userMapper.selectById(usrId);
    }

    @Override
    public IPage<User> getUserByPage(HashMap<String, Object> hashMap) {
        Integer pageNo = (Integer) hashMap.get("pageIndex");
        Integer pageSize = (Integer) hashMap.get("pageSize");
        Page<User> page = new Page<>(pageNo, pageSize);
        IPage<User> userPager = userMapper.getUserByPage(page, hashMap);
        return userPager;
    }
}
